package com.inspur.attendance.Controller;

import com.inspur.attendance.Entity.Daily;
import com.inspur.attendance.Service.DailyService;
import com.inspur.attendance.Tools.Return;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class DailyController {

    @Autowired
    DailyService dailyService;

    /**
     * 获取所有的日报
     * @return
     */
    @RequestMapping("/getAllDaily")
    public Return getAllDaily(@Param("pageNo") int pageNo,
                              @Param("pageSize") int pageSize) {
        System.out.println(dailyService.getAllDaily(pageNo, pageSize));
        return Return.success(dailyService.getAllDaily(pageNo, pageSize));
    }


    /**
     * 删除日报
     * @param id
     * @return
     */
    @RequestMapping("/delDaily")
    public Return delDaily(@RequestParam("id") Long id) {
        try {
            dailyService.delDaily(id);
            return Return.success("删除成功");
        }catch(Exception e) {
            return Return.failure();
        }
    }

    /**
     * 添加日报
     * @param
     * @return
     */
    @RequestMapping("/addDaily")
    public Return addDaily(@Param("account") String account,
                           @Param("applicant") String applicant,
                           @Param("other") String other,
                           @Param("problem") String problem,
                           @Param("tomorrow_plan") String tomorrow_plan,
                           @Param("work_content") String work_content,
                           @Param("work_process") String work_process) {
        Daily daily = new Daily();
        daily.setAccount(account);
        daily.setApplicant(applicant);
        daily.setCreateTime(new Date());
        daily.setOther(other);
        daily.setProblem(problem);
        daily.setTomorrow_plan(tomorrow_plan);
        daily.setWork_content(work_content);
        daily.setWork_process(work_process);
        System.out.println(daily);
        try {
            dailyService.addDaily(daily);
            return Return.success("添加成功");
        }catch (Exception e) {
            return Return.failure();
        }
    }

    @RequestMapping("/editDaily")
    public Return editDaily(@Param("account") String account,
                           @Param("applicant") String applicant,
                           @Param("other") String other,
                           @Param("problem") String problem,
                           @Param("tomorrow_plan") String tomorrow_plan,
                           @Param("work_content") String work_content,
                           @Param("work_process") String work_process,
                            @Param("id") Long id) throws Exception {
        Daily daily = dailyService.findById(id);
        daily.setAccount(account);
        daily.setApplicant(applicant);
        daily.setCreateTime(new Date());
        daily.setOther(other);
        daily.setProblem(problem);
        daily.setTomorrow_plan(tomorrow_plan);
        daily.setWork_content(work_content);
        daily.setWork_process(work_process);
        System.out.println(daily);
        try {
            dailyService.editDaily(daily);
            return Return.success("添加成功");
        }catch (Exception e) {
            return Return.failure();
        }
    }



    /**
     * 按照时间查询日报
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws IllegalAccessException
     */
    @RequestMapping("/findByDate")
    public Return findByDate(@RequestParam("startTime") String startTime,
                             @RequestParam("endTime") String endTime,
                             @RequestParam("pageNo") int pageNo,
                             @RequestParam("pageSize") int pageSize) throws IllegalAccessException {
        try {
            System.out.println(pageNo);
            return Return.success(dailyService.findByDate(startTime, endTime, pageNo, pageSize));
        }catch (Exception e) {
            return Return.failure();
        }
    }

    /**
     * 按名字查询
     * @param
     * @return
     */
    @RequestMapping("/findByName")
    public Return findByName(@RequestParam(value = "name") String name) {
        try {
            return Return.success(dailyService.findByName(name));
        }catch (Exception e) {
            return Return.failure();
        }
    }
}
